package iosu.java.games.razer.api;

import iosu.java.games.razer.server.AlreadyBoundCar;
import iosu.java.games.razer.server.GameStatus;

import java.util.Date;

/**
 * esta clase implementa una partida
 * @author iosu
 * 
 */
public class Game {
	public static final int FIRST_POSITION = 0;
	public static final int SECOND_POSITION = 1;
	public static final int THIRD_POSITION = 2;
	public static final int FOURTH_POSITION = 3;
	// 4 participantes
	private Client car1;
	private Client car2;
	private Client car3;
	private Client car4;
	// hora de creacion de la partida
	private Date creationtime;
	// hora de inicio de la partida
	private Date startingtime;
	// hora de fin de la partida
	private Date finishingtime;
	// id de la partida
	private Integer gameid;
	private String ownerid;
	private int numplayers;
	private GameStatus gameStatus;
	private int[] results = new int[4];
	private boolean gameFinished;

	public Client getCar1() {
		if (car1 != null)
			if (car1.getCar() == null)
				car1.setCar(CarInfo.defaultCar1());
		return car1;
	}

	public void setCar1(Client owner) throws AlreadyBoundCar {
		if (this.car1 == null) {
			this.car1 = owner;
		} else {
			throw new AlreadyBoundCar("car1/owner ya esta asignado");
		}
	}

	public Client getCar2() {
		if (car2 != null)
			if (car2.getCar() == null)
				car2.setCar(CarInfo.defaultCar2());
		return car2;
	}

	public void setCar2(Client car2) throws AlreadyBoundCar {
		if (this.car2 == null) {
			this.car2 = car2;
		} else {
			throw new AlreadyBoundCar("car2 ya esta asignado");
		}
	}

	public void setCar3(Client car3) throws AlreadyBoundCar {
		if (this.car3 == null) {
			this.car3 = car3;
		} else {
			throw new AlreadyBoundCar("car3 ya esta asignado\n" + toString());
		}
	}

	public Client getCar3() {
		if (car3 != null)
			if (car3.getCar() == null)
				car3.setCar(CarInfo.defaultCar3());
		return this.car3;
	}

	public Client getCar4() {
		if (car4 != null)
			if (car4.getCar() == null)
				car4.setCar(CarInfo.defaultCar4());
		return car4;
	}

	public void setCar4(Client car4) throws AlreadyBoundCar {
		if (this.car4 == null) {
			this.car4 = car4;
		} else {
			throw new AlreadyBoundCar("car4 ya esta asignado\n" + toString());
		}
	}

	public Date getStartingtime() {
		return startingtime;
	}

	public void setStartingtime(Date startingtime) {
		this.startingtime = startingtime;
	}

	public Date getFinishingtime() {
		return finishingtime;
	}

	public void setFinishingtime(Date finishingtime) {
		this.finishingtime = finishingtime;
	}

	public Integer getGameid() {
		return gameid;
	}

	public void setGameid(Integer gameid) {
		this.gameid = gameid;
	}

	public String getOwnerid() {
		return ownerid;
	}

	public void setOwnerid(String ownerid) {
		this.ownerid = ownerid;
	}

	public void updateCar1(Client c) {
		this.car1 = c;
	}

	public void updateCar2(Client c) {
		this.car2 = c;
	}

	public void updateCar3(Client c) {
		this.car3 = c;
	}

	public void updateCar4(Client c) {
		this.car4 = c;
	}

	public int getNumberOfPlayers() {
		int resp = 0;
		if (car1 != null)
			resp++;
		if (car2 != null)
			resp++;
		if (car3 != null)
			resp++;
		if (car4 != null)
			resp++;
		return resp;
	}

	@Override
	public String toString() {
		return "Game [car1=" + car1 + ", car2=" + car2 + ", car3=" + car3
				+ ", car4=" + car4 + ", startingtime=" + startingtime
				+ ", finishingtime=" + finishingtime + ", gameid=" + gameid
				+ ", ownerid=" + ownerid + ", numplayers=" + numplayers + "]";
	}

	public GameStatus getGameStatus() {
		return this.gameStatus;
	}

	public Date getCreationtime() {
		return creationtime;
	}

	public void setCreationtime(Date creationtime) {
		this.creationtime = creationtime;
	}

	public void setGameStatus(GameStatus gameStatus) {
		this.gameStatus = gameStatus;
	}

	/**
	 * este metodo finaliza un coche metiendolo en el array de resultados en la
	 * primera posicion libre que haya
	 * 
	 * @param car
	 *            es el id de servidor del coche
	 */
	public void finishCar(int car) {
		// el array se inicializa a 0, asi que recorro miuentras no sea 0 e
		// inserto en el primero que lo sea
		// System.out.println("finalizando coche "+car);
		int t = 0;
		for (int i = 0; i < 4; i++) {
			if (this.results[i] == 0) {
				// System.out.println("el coche "+car+" ha quedado en el puesto "+(i+1));
				this.results[i] = car;
				// y ademas marco el coche como finalizado
				if (car1.getClientid() == car) {
					car1.setFinished();
				} else if (car2.getClientid() == car) {
					car2.setFinished();
				} else if (car3.getClientid() == car) {
					car3.setFinished();
				} else if (car4.getClientid() == car) {
					car4.setFinished();
				}
				break;
			} else {
				// System.out.println("la posicion "+(i+1)+"ya esta ocupada");
				t++;
			}
		}
		if (t >= 3) {
			// System.out.println("finalizando partida "+this.gameid);
			this.gameFinished = true;
		} else {
			// System.out.println("aun faltan "+(3-t));
		}

	}

	public boolean isGameFinished() {
		return this.gameFinished;
	}

	public int getGameResults(int i) {
		if (this.gameFinished) {
			// i es la posicion por la uqe pregunto
			return this.results[i];
		} else {
			// si la partida no ha terminado, contesto con 0
			return 0;
		}
	}

}
